import React from 'react'
import PropTypes from 'prop-types'
import Component, { mapStateToProps, mapDispatchToProps } from '@components/Component'
import { connect } from 'react-redux'
import { message } from 'antd'
import Approve from '@components/ApprovePre/Index'
import StepOneInput from './components/StepOneInput'
import StepThreeInput from './components/StepThreeInput'
import StepOneHisotry from './components/StepOneHistory'
import StepThreeHistory from './components/StepThreeHistory'
// import { requestFiles } from '@js/request'
// import { awaitWrap } from '@js/tool'

class CloseCase extends Component {
  state = {
    dictionaries: {}
  }

  beforeSubmit (instance, data) {
    switch (instance.currentStep.step_key) {
      case 'zcybaochou01':
        return this.oneStepSubmit(instance, data)
      case 'zcybaochou03':
        return this.threeStepSubmit(instance, data)
      default:
        return data
    }
  }

  // 保存报酬列表
  async updateFeeList (instance, list) {
    const hide = message.loading('保存酬金列表中', 120)
    return new Promise((resolve, reject) => {
      this.$put('/api/xapc/remuneration/saveall', { list, caseid: this.props.caseDetail.id, flowid: instance.id })
        .then(res => {
          hide()
          resolve()
        })
        .catch(e => {
          hide()
          reject(e)
        })
    })
  }

  async oneStepSubmit (instance, data) {
    await this.updateFeeList(instance, data.vars.remuneration_list01)
    data.vars.remuneration_list01 = JSON.stringify(data.vars.remuneration_list01)
    return data
  }

  async threeStepSubmit (instance, data) {
    await this.updateFeeList(instance, data.vars.remuneration_list)
    data.vars.remuneration_list = JSON.stringify(data.vars.remuneration_list)
    return data
  }

  componentWillMount () {

  }

  // 渲染当前操作步骤线上部分
  renderCurrentStepLineTop (formRef, instance, renderFn) {
    switch (instance.currentStep.step_key) {
      case 'zcybaochou01':
        return <StepOneInput instance={instance} userInfo={this.props.userInfo} caseDetail={this.props.caseDetail} formRef={formRef} dictionaries={this.state.dictionaries} />
      case 'zcybaochou03':
        return <StepThreeInput instance={instance} userInfo={this.props.userInfo} caseDetail={this.props.caseDetail} formRef={formRef} dictionaries={this.state.dictionaries} />
      default:
        return renderFn()
    }
  }

  // 渲染历史
  renderHistory (action, renderFn, instance, renderDocument) {
    const key = action.step_key
    // const isDisagree = action.opt_add === 'disagree'
    switch (key) {
      case 'zcybaochou01':
        return <StepOneHisotry key={action.id} userInfo={this.props.userInfo} caseDetail={this.props.caseDetail} instance={instance} action={action} renderDocument={renderDocument} />
      case 'zcybaochou03':
        return <StepThreeHistory key={action.id} userInfo={this.props.userInfo} caseDetail={this.props.caseDetail} instance={instance} action={action} renderDocument={renderDocument} />
    }
    return renderFn(action)
  }

  render () {
    return (
      <div>
        <Approve
          caseDetail={this.props.caseDetail}
          flowKey='zcybaochou'
          afterAction={(instance, action) => this.props.refreshCaseData()}
          creatable={this.props.editable}
          renderCurrentStepLineTop={(...params) => this.renderCurrentStepLineTop(...params)}
          beforeSubmit={(...params) => this.beforeSubmit(...params)}
          renderHistory={(h, renderFn, instance, renderDocument) => this.renderHistory(h, renderFn, instance, renderDocument)}
        />
      </div>
    )
  }
}

CloseCase.propTypes = {
  caseDetail: PropTypes.object
  // active: PropTypes.bool,
  // refreshCaseData: PropTypes.func
  // editable: PropTypes.bool,
  // fileList: PropTypes.array
  // getFileList: PropTypes.func
}
CloseCase.defaultProps = {
  caseDetail: {},
  active: false,
  refreshCaseData: () => {},
  editable: false,
  fileList: [],
  getFileList: () => {}
}

const mapState = mapStateToProps(function (state) {
  return {
    userInfo: state.basic.userInfo
  }
})

const mapDispatch = mapDispatchToProps(function (dispatch) {
  return {

  }
})

export default connect(mapState, mapDispatch)(CloseCase)
